import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sainbainu/common/routes/routes.dart';
import 'package:sainbainu/common/values/colors.dart';
import 'package:sainbainu/common/widgets/widget.dart';

import 'index.dart';
import 'widgets/widgets.dart';

class LoginPage extends GetView<LoginController> {
  const LoginPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<LoginController>(
      builder: (_) {
        return KeyboardClose(
          child: Scaffold(
            body: Stack(
              children: [
                _buildBodyWrapper(
                  top: Obx(
                    () => AnimatedSwitcher(
                      duration: const Duration(milliseconds: 300),
                      child: _buildloginPage(_.state.curPageType.value),
                    ),
                  ),
                  bottom: Obx(
                    () => ThirdPartyLoginBtns(
                      showWechat: _.state.showWechat.value,
                      onWechatLogin: () => _.handleWeChatLogin(),
                      onAppleLogin: () => _.handleAppleLogin(),
                    ),
                  ),
                ),
                SafeArea(
                  left: false,
                  bottom: false,
                  right: false,
                  child: Padding(
                    padding: const EdgeInsets.only(left: 10),
                    child: IconButton(
                      onPressed: () => Get.back(),
                      iconSize: 28,
                      icon: const Icon(Icons.close_rounded),
                    ),
                  ),
                ),
              ],
            ),
            bottomNavigationBar: BottomAgree(
              onSwitchAgree: _.switchAgree,
              onUserAgree: () => Get.toNamed(AppRoutes.userAgree),
              onPrivacyAgree: () => Get.toNamed(AppRoutes.privacyAgree),
              onAuthAgree: () {},
            ),
          ),
        );
      },
    );
  }

  Widget _buildBodyWrapper({
    required Widget top,
    required Widget bottom,
  }) {
    return CustomScrollView(
      slivers: [
        SliverToBoxAdapter(
          child: SafeArea(
            bottom: false,
            child: Padding(
              padding: const EdgeInsets.only(top: kToolbarHeight),
              child: FractionallySizedBox(
                widthFactor: 0.65,
                child: top,
              ),
            ),
          ),
        ),
        SliverFillRemaining(
          hasScrollBody: false,
          child: Align(
            alignment: Alignment.bottomCenter,
            child: Padding(
              padding: const EdgeInsets.only(bottom: 30),
              child: bottom,
            ),
          ),
        ),
      ],
    );
  }

  /// 登陆类型
  Widget _buildloginPage(PageType curPageType) {
    switch (curPageType) {
      case PageType.eazyLogin:
        return EazyLogin(
          onLogin: () => controller.handelEazyLogin(),
          onCodeLogin: () => controller.switchPageType(PageType.codeLogin),
        );
      case PageType.codeLogin:
        return CodeLogin(
          onCodeBtnPressed: () => controller.sendCode(),
          onLogin: () => controller.handleSmsCodeLogin(),
          onEazyLogin: () => controller.switchPageType(PageType.eazyLogin),
          onPwdLogin: () => controller.switchPageType(PageType.pwdLogin),
        );
      case PageType.pwdLogin:
        return PwdLogin(
          onLogin: () => controller.handlePasswordLogin(),
          onCodeLogin: () => controller.switchPageType(PageType.codeLogin),
          onEazyLogin: () => controller.switchPageType(PageType.eazyLogin),
        );
      // case PageType.findPwd:
      //   return FindPwd(
      //     onCodeBtnPressed: () {},
      //     onSubmit: () => controller.switchPageType(PageType.resetPwd),
      //     onPwdLogin: () => controller.switchPageType(PageType.pwdLogin),
      //   );
      // case PageType.resetPwd:
      //   return ResetPwd(
      //     onSubmit: () {},
      //     onEazyLogin: () => controller.switchPageType(PageType.eazyLogin),
      //     onPwdLogin: () => controller.switchPageType(PageType.pwdLogin),
      //   );
      default:
        return const SizedBox.shrink();
    }
  }
}
